
package com.huaweicloud.haydn.delivercore.agent.service.metadata.impl;

import com.huaweicloud.haydn.delivercore.agent.common.exception.BusinessErrorEnum;
import com.huaweicloud.haydn.delivercore.agent.common.exception.BusinessException;
import com.huaweicloud.haydn.delivercore.agent.dto.excel.DbExcelDto;
import com.huaweicloud.haydn.delivercore.agent.dto.excel.DbLogExcelDto;
import com.huaweicloud.haydn.delivercore.agent.dto.excel.FieldExcelDto;
import com.huaweicloud.haydn.delivercore.agent.dto.excel.TableExcelDto;
import com.huaweicloud.haydn.delivercore.agent.service.metadata.DatabaseUtil;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @since 2022-09-26
 */
@Data
@Slf4j
public abstract class BaseDatabaseUtil implements Cloneable, DatabaseUtil {

    private String hostname;

    private String port;

    private String databaseName;

    private String username;

    private String password;

    private String dbtype;

    private String selectedDbName;

    private String selectedTableNames;

    @Override
    public void init(String dbHostname, String dbPort, String dbDatabaseName, String dbUsername, String dbPassword,
        String databaseType, String dbNameStr, String tableNamesStr) {
        hostname = dbHostname;
        port = dbPort;
        databaseName = dbDatabaseName;
        username = dbUsername;
        password = dbPassword;
        dbtype = databaseType;
        selectedDbName = dbNameStr;
        selectedTableNames = tableNamesStr;
    }

    @Override
    public String getURL(String hostname, String port, String databaseName) {
        log.info("");
        return null;
    }

    @Override
    public String getURLException(String hostname, String port, String databaseName) {
        log.info("");
        return null;
    }

    @Override
    public String getDriverClass() {
        log.info("");
        return null;
    }

    @Override
    public String getAllDatabasesSql() throws BusinessException {
        log.info("");
        return null;
    }

    @Override
    public String getAllSchemasSql() {
        log.info("");
        return null;
    }

    @Override
    public String getAllTablesSql() {
        log.info("");
        return null;
    }

    @Override
    public List<String> getAllSchemas(int firstSize, int maxSize) throws BusinessException {
        log.info("");
        return null;
    }

    @Override
    public List<String> getAllDatabases(int firstSize, int maxSize) throws BusinessException {
        log.info("");
        return null;
    }

    @Override
    public long getSchemaNum() throws BusinessException {
        log.info("");
        return 0;
    }

    @Override
    public long getDatabasesNum() throws BusinessException {
        log.info("");
        return 0;
    }

    @Override
    public List<String> getAllTables(int firstSize, int maxSize, String schema) throws BusinessException {
        log.info("");
        return null;
    }

    @Override
    public long getTableNum(String schema) throws BusinessException {
        log.info("");
        return 0;
    }

    @Override
    public Connection getConnection() throws BusinessException {

        // 获取数据库连接URL
        String url;
        try {
            url = getURL(hostname, port, databaseName);
            // 加载驱动
            Class.forName(getDriverClass());
            // 创建数据库连接
            return DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException | SQLException e) {
            if (e.getMessage().contains("ORA-12505")) {
                url = getURLException(hostname, port, databaseName);
                // 加载驱动
                try {
                    Class.forName(getDriverClass());
                    return DriverManager.getConnection(url, username, password);
                } catch (ClassNotFoundException | SQLException ex) {
                    log.error("getConnection end. database load driver failed.", ex);
                    throw new BusinessException(BusinessErrorEnum.BASE_INTERNAL_SERVER_ERROR);
                }

            } else {
                log.error("getConnection end. database load driver failed.", e);
                throw new BusinessException(BusinessErrorEnum.BASE_INTERNAL_SERVER_ERROR);
            }

        }
    }

    @Override
    public long executeCountSql(String countSql) {
        log.info("");
        return 0;
    }

    protected static void close(ResultSet rs, PreparedStatement ps, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            log.error("close db end. exception:{}", e);
        }
    }

    @Override
    public List<DbExcelDto> getAllDbs(String datalinkName, String dbNames, String schemas) {
        log.info("");
        return null;
    }

    @Override
    public List<TableExcelDto> getAllTables(String datalinkName, String dbNames, String schemas, String tableNames) {
        log.info("");
        return null;
    }

    @Override
    public List<FieldExcelDto> getAllFields(String datalinkName, String dbNames, String schemas, String tableNames) {
        log.info("");
        return null;
    }

    @Override
    public List<DbLogExcelDto> genLogs() {
        log.info("");
        return null;
    }
}
